package handler

import (
	amodel "funlog-gserver/model/api"
	model "funlog-gserver/model/db"
	"funlog-gserver/stored/db"
	"strings"

	"github.com/gin-gonic/gin"
	"github.com/gin-gonic/gin/binding"
	"github.com/go-basic/uuid"
)

//ApplicationAddHandler 添加新应用
func ApplicationAddHandler(c *gin.Context) {
	var json amodel.ApplicationInfo
	err := c.ShouldBindBodyWith(&json, binding.JSON)
	if err != nil {
		SendResponse(c, QUERY_PARAMS_ERROR, "参数错误", nil)
		return
	}
	uid, _ := GetUser(c)
	uuid := strings.Replace(uuid.New(), "-", "", -1)
	app := model.Application{UID: uid, APPKEY: uuid, TID: json.TID, Name: json.Name, AppType: json.AppType, Describe: json.Describe, Polymerization: json.Polymerization}
	result := db.DB.Create(&app)
	if result.Error != nil {
		//如果是触发了索引
		SendResponse(c, DB_INSERT_ERROR, "系统错误:"+result.Error.Error(), nil)
	}
	SendResponse(c, SUCCESS, "ok", app)

}

//ApplicationGetListHandler 获取用户的应用列表
func ApplicationGetListHandler(c *gin.Context) {
	var json amodel.PageInfo
	err := c.ShouldBindQuery(&json)
	if err != nil {
		SendResponse(c, QUERY_PARAMS_ERROR, "参数错误", nil)
		return
	}
	uid, user := GetUser(c)
	//返回的app 集合
	apps := []model.Application{}
	//跳过多少条
	offset := (json.PageNum - 1) * json.Limit
	//查询应用
	result := db.DB.Where("(uid=? or tid in ?) and status =1", uid, user["teamIds"]).Limit(json.Limit).Offset(offset).Find(&apps)
	if result.Error != nil {
		goto SYSERROR
	}
	SendResponse(c, SUCCESS, "ok", apps)
	return
SYSERROR: //系统出错
	SendResponse(c, SYATEM_ERROR, "系统错误", nil)
}

//ApplicationGetHandler 获取某个应用信息
func ApplicationGetHandler(c *gin.Context) {
	appId := c.Param("id")
	uid, user := GetUser(c)
	app := model.Application{}
	teamIds := user["teamIds"]
	//根据创建用户id和应用所在团队查询应用信息
	result := db.DB.Where("id=? and (uid=? or tid in ?) and status =1", appId, uid, teamIds).Find(&app)
	if result.Error != nil {
		goto SYSERROR
	}
	if result.RowsAffected == 0 {
		SendResponse(c, DB_QUERY_ERROR, "查询失败", nil)
	} else {
		SendResponse(c, SUCCESS, "ok", app)
	}
	return
SYSERROR: //系统出错
	SendResponse(c, SYATEM_ERROR, "系统错误", nil)
}

//ApplicationPutHandler 更新应用信息
func ApplicationPutHandler(c *gin.Context) {
	var json amodel.ApplicationInfo
	aid := c.Param("id")
	err := c.ShouldBindBodyWith(&json, binding.JSON)
	if err != nil {
		SendResponse(c, QUERY_PARAMS_ERROR, "参数错误", nil)
		return
	}
	uid, _ := GetUser(c)
	app := model.Application{TID: json.TID, Name: json.Name, AppType: json.AppType, Describe: json.Describe, Polymerization: json.Polymerization}
	//更新应用
	result := db.DB.Where("uid = ? and id = ?  and status =1", uid, aid).Updates(&app)
	if result.Error != nil {
		SendResponse(c, SYATEM_ERROR, "系统错误", nil)
		return
	} else if result.RowsAffected == 0 {
		SendResponse(c, DB_UPDATE_ERROR, "更新失败", nil)
		return
	}
	SendResponse(c, SUCCESS, "ok", nil)
}

//ApplicationDelHandler  删除应用，非物理删除，逻辑删除
func ApplicationDelHandler(c *gin.Context) {
	aid := c.Param("id")
	uid, _ := GetUser(c)
	//更新应用
	result := db.DB.Model(&model.Application{}).Where("uid = ? and id = ?  and status =1", uid, aid).Update("status", 0)
	if result.Error != nil {
		SendResponse(c, SYATEM_ERROR, "系统错误", nil)
		return
	} else if result.RowsAffected == 0 {
		SendResponse(c, DB_DELETE_ERROR, "删除失败", nil)
		return
	}
	SendResponse(c, SUCCESS, "ok", nil)
}
